import Vue from "vue";
import Vuex from "vuex";
import {request} from "@/helper/request";
import setItem from "@/helper/setItem";
import getItem from "@/helper/getItem";
import {copy} from "@/helper/copy";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: null,
    showLoading: false,
    showEmpty: false
  },
  mutations: {
    showLoading(state, payload) {
      state.showLoading = payload;
    },
    showEmpty(state, payload) {
      state.showEmpty = payload;
    },
    saveUser(state, payload) {
      setItem("supplyUser", JSON.stringify(payload.user));
    },
    getUser(state) {
      state.user = copy(getItem("supplyUser"));
    },
    removeUser() {
      window.localStorage.removeItem("supplyUser");
    },
  },
  actions: {
    async getResponse(context, payload) {
      return await request(payload.url, payload.method, payload.value);
    },
  },
});
